// pages/myaddress3/myaddress3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,//修改地址弹出层    初始值为false
    addressid:'',
    dataList:[],
    transferTime:'',
    orderid:'',
    remarks:'',
    phone:'',
    name:'',
    address:'',
    newProductid:'',
  },
  // 查询用户的默认地址
  getdefaultAddress(){
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/address/select',
      data: {
        userid:getApp().globalData.openid,
        page:1,
        size:100
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        var addresL = res.data.data
        if(res.data.flag){
          if(addresL.length < 1){
            wx.showToast({
              title: '暂无地址信息，请到个人中心添加地址！',
              icon:'none',
              duration:1000
            })
            return
          }
          for(var i = 0;i < addresL.length;i++){
            if(addresL[i].prefer == '1'){
              that.setData({
                addressid: addresL[i].addressid,
                address: addresL[i].address,
                name: addresL[i].name,
                phone: addresL[i].phone
              })
            }else{
             
            }
          }
          
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  // 备注信息
  inputRemarks(event){
    this.setData({
      remarks:event.detail.value
    })
  },
  // 提交下单数据
  takeOrder(){
    var that = this
    var mydata = {
      fulWagoOrder:{
                      userid:getApp().globalData.openid,
                      status:1,
                      productNum:1,
                      productJifen:that.data.dataList.jifen,
                      cannel:2,
                      addressid:that.data.addressid,
                      remarks:that.data.remarks,
                      productid:that.data.dataList.productid,
                      expressnumber:""
      },
      newProductid:that.data.newProductid
  }    
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/order/add',
      data: mydata,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        wx.showToast({
          title: res.data.message,
          icon:'none'
        })
        if(res.data.flag){
          wx.showToast({
            title: '下单成功!',
            duration:500
          })
          
          
          wx.requestSubscribeMessage({
            tmplIds: ['ru3cMSQKFurYaBL_Czpnv9xVzMFWUhVeqMwxDMWabXE'],
            success (res) { 
              
            }
          })
          
          // setTimeout(function () {
          //   wx.navigateTo({
          //     url: '/pages/myApplication/myApplication?status=2',
          //   })
          // }, 5000);
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  // 时间格式转换
  transferTime(value) {
    var date = new Date(value);
    var Y = date.getFullYear() + "-";
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var ho = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var mi = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var se = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    var time = Y + M + D + ' ' + ho + ":" + mi + ":" + se;
    return time;
  },
  // 修改地址
  tochangeEdit(){
    wx.navigateTo({
      url: '/pages/orderEditAddress2/orderEditAddress2?addressid=' + this.data.addressid
    })
  },
  // 通过修改地址页面传过来的addressid查询地址的信息
  getAddressData(){
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/address/selectByID',
      data: {
        addressid:that.data.addressid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        var addresL = res.data.data
        that.setData({
          address: addresL.address,
          name: addresL.name,
          phone: addresL.phone
        })
        
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mydatalist = JSON.parse(options.item)
    this.setData({
      dataList:mydatalist,
      transferTime:mydatalist.transferTime,
      orderid:mydatalist.orderid,
      newProductid:mydatalist.newProduct.id
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.addressid != ''){
      this.getAddressData()   //通过修改地址页面传过来的addressid查询此地址信息
    }else{
      // 查用户的默认地址
      this.getdefaultAddress()
    }

    //当前页面
    let pages = getCurrentPages();
    
    //上一页面
    let prevPage = pages[pages.length - 2];
    
    //将数值信息赋值给上一页面sta变量
    prevPage.setData({
      sta: 2
    });
    
    // //关闭当前界面返回到上一页面
    // wx.navigateBack({
    // //返回栈数
    //   delta: 1
    // })
    
  },
  

})